package com.annotation.config;

import com.annotation.importannotation.MyImportBeanDefinitionRegistor2;
import com.annotation.importannotation.MyImportBeanDefinitionRegistrar;
import com.annotation.importannotation.MyImportSeletor;
import com.annotation.conditional.LinuxCondition;
import com.annotation.conditional.WindowCondition;
import com.annotation.entities.Color;
import com.annotation.entities.Person;
import com.annotation.entities.Red;
import com.annotation.importannotation.MyImportSeletor2;
import com.annotation.factorybean.ColorFactoryBean;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.*;

/**
 * 在容器中注册组建
 *      1:包扫描：带用注解的组建(@Controller、@Service、@Repotitory...)
 *      2:bean注解导入组建【导入第三方包里的组建】
 *      3:@Import快速向容器中导入组建
 *          1.@Import(需要导入的组建)；容器中会自动注册组建，默认id是全类名
 *          2.ImportSelector返回需要导入的组建全类名数组，不可以自定义组建id，全类名
 *          3.ImportBeanDefinitionRegistrar：手动注册bean到容器中，可以自定义组建的id
 *      4:自己定义一个类实现FactoryBean接口，实现其方法，指定要注册到容器中的bean
 */
//@ComponentScan(value = "com.annotation",useDefaultFilters = false,includeFilters = {@ComponentScan.Filter(type = FilterType.CUSTOM,classes = MyTypeFilter.class)})
/**
 * @Import注解导入的组建id默认是全类名
 */
@Import(value = {Color.class,Red.class,MyImportSeletor.class,MyImportBeanDefinitionRegistrar.class,MyImportSeletor2.class,MyImportBeanDefinitionRegistor2.class})
@Configuration
//将conditional注解放在类上，若不满足条件，则类中所有的bean都不会被注册到IOC容器中
//@Conditional({WindowCondition.class})
public class MainConfig2 {

    @Bean
    public ColorFactoryBean colorFactoryBean(){
        return new ColorFactoryBean();
    }

    //这种方法将ImportSelector类注入到容器中是行不通的，它返回的数组中的类并不会被注册到容器中
    /*@Bean
    public MyImportSeletor seletor(){
        return new MyImportSeletor();
    }*/


//    @Scope(value = WebApplicationContext.SCOPE_REQUEST)//request
//    @Scope(value = WebApplicationContext.SCOPE_GLOBAL_SESSION)//session
//    @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)//prototype
    @Lazy
    @Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)//singleton
    @Bean(name = "person")
    public Person person(){
        Person person = new Person();
        person.setName("lqf");
        person.setEmail("lqf@163.com");
        return person;
    }

    /**
     * @Conditational按照条件给IOC容器中注册bean
     */
    @Conditional({WindowCondition.class})
    @Bean(name = "bill")
    public Person person01(){
        return new Person(1,"bill","bill@email");
    }

    @Conditional({LinuxCondition.class})
    @Bean(name = "linus")
    public Person person02(){
        return new Person(2,"linus","linus@email");
    }

}
